const Koa = require('koa')
// const bodyparser = require('koa-bodyparser') // post参数解析
const koaBody = require('koa-body')
const cors = require('koa2-cors')
const koaStatic = require('koa-static')
const parameter = require('koa-parameter')
const path = require('path');
const app = new Koa(); // 实例化
// 引入路由
const UserRouter = require('./router/users')

app.use(koaStatic(path.join(__dirname, 'public')));

app.use(koaBody({
  multipart: true, // 支持文件上传
  // encoding: 'gzip',
  formidable: {
    uploadDir: path.join(__dirname, 'public/uploads/'), // 设置文件上传目录
    keepExtensions: true,    // 保持文件的后缀
    maxFieldsSize: 2 * 1024 * 1024, // 文件上传大小
    onFileBegin: (name, file) => { // 文件上传前的设置
      // console.log(`name: ${name}`);
      // console.log(file);
    },
  }
}));

app.use(cors())
// app.use(bodyparser())
app.use(UserRouter.routes(), UserRouter.allowedMethods())

app.listen(3000, () => {
  console.log(`server port is 3000`)
})